
package com.dome.Thread1;
//多线程卖票案例
public class Ticket implements Runnable {
    //设定参数
    private int ticket=100;
    //设定同步代码块锁的对象,如果是同步方法,锁对象就是this,如果是静态方法,锁对象就是类名.class
    Object lock=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (lock){
                if (ticket==0){
                    break;
                }else {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ticket--;
                    System.out.println(Thread.currentThread().getName()+"正在卖票,还剩"+ticket+"张");
                }
            }
        }
    }
}
